package com.ssyc.gsk_teacher.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.activity.TeacherQueryHomeWorkActivity;
import com.ssyc.gsk_teacher.bean.HistoryHomeWork;

import java.util.ArrayList;
import java.util.List;

/**
 * 教师端--历史练习--列表适配器
 * Created by Administrator on 2018/5/8 0008.
 */

public class TeaHomeWorkRvAdapter extends BaseQuickAdapter<HistoryHomeWork.HomeworkBean, BaseViewHolder> {

    private Context context;

    public TeaHomeWorkRvAdapter(Context context, int layoutResId, @Nullable List<HistoryHomeWork.HomeworkBean> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolder holder, HistoryHomeWork.HomeworkBean info) {

        //创建时间
        TextView tvCreateTime = holder.getView(R.id.tv_create_time);
        tvCreateTime.setText(info.getCreated());

        //根据服务器返回的状态来决定APP练习和题库练习的整体布局显示状态
        //APP练习的整体布局
        LinearLayout llAppHomework = holder.getView(R.id.ll_app_homework);

        //上次练习点评的整体布局
        LinearLayout llLastHomeWork = holder.getView(R.id.ll_last_homework);

        //上次练习点评
        TextView tvLastHomeWork = holder.getView(R.id.tv_last_homework);

        //本次练习的线性布局
        LinearLayout llThisHomeWork = holder.getView(R.id.ll_this_home_work);

        //本次练习
        TextView tvThisHomeWork = holder.getView(R.id.tv_this_homework);

        //显示布置练习图片的RecyclerView
        RecyclerView rv = holder.getView(R.id.rv);


        //只有题库练习的时候不显示APP的练习
        if (TextUtils.isEmpty(info.getCONTENT()) && TextUtils.isEmpty(info.getCOMMENT()) && info.getImages() == null) {
            Log.i("test", "走了隐藏APP练习条目的方法");
            //APP整体练习也不显示
            llAppHomework.setVisibility(View.GONE);
        } else {
            //APP练习显示
            llAppHomework.setVisibility(View.VISIBLE);
            //上次练习点评是否显示
            if (TextUtils.isEmpty(info.getCOMMENT())) {
                //上次点评为空
                llLastHomeWork.setVisibility(View.GONE);
            } else {
                llLastHomeWork.setVisibility(View.VISIBLE);
                tvLastHomeWork.setText(info.getCOMMENT());
            }

            //本次练习是否显示的操作
            //获取本次练习的图片列表集合
            List<String> homeWorkUrls = getThisHomeWorkImages(info.getImages());

            //本次练习评论为空并且本次练习的图片Url集合也为空,不显示本次练习的整体布局
            if (TextUtils.isEmpty(info.getCONTENT()) && (homeWorkUrls == null || homeWorkUrls.size() == 0)) {
                //不显示本次练习的整体布局
                llThisHomeWork.setVisibility(View.GONE);
            } else {
                //显示本次练习的整体布局
                llThisHomeWork.setVisibility(View.VISIBLE);
                //本次练习点评是否显示
                if (TextUtils.isEmpty(info.getCONTENT())) {
                    tvThisHomeWork.setVisibility(View.GONE);
                } else {
                    tvThisHomeWork.setVisibility(View.VISIBLE);
                    tvThisHomeWork.setText(info.getCONTENT());
                }

                //本次练习点评的图片展示
                if (homeWorkUrls == null || homeWorkUrls.size() == 0) {
                    rv.setVisibility(View.GONE);
                } else {
                    rv.setVisibility(View.VISIBLE);
                    LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
                    rv.setLayoutManager(manager);
                    rv.setHasFixedSize(true);
                    //练习的图片
                    RvShowPicAdapter adapter = new RvShowPicAdapter(context, R.layout.teacher_rv_show_homework, homeWorkUrls);
                    rv.setAdapter(adapter);
                }
            }
        }

        llAppHomework.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //查看练习布置详情
                Intent intent = new Intent(context, TeacherQueryHomeWorkActivity.class);
                intent.putExtra("classNum", info.getUSER_CLASS()+"");
                intent.putExtra("workId", info.getROW_ID()+"");
                context.startActivity(intent);

            }
        });
    }

    /**
     * 返回本次练习的图片集合方法
     *
     * @param images
     * @return
     */
    private List<String> getThisHomeWorkImages(HistoryHomeWork.HomeworkBean.ImagesBean images) {
        List<String> mUrls = null;
        if (images != null) {
            mUrls = new ArrayList<>();
            if (!TextUtils.isEmpty(images.getUSER_PICTURE1())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE1());
            }
            if (!TextUtils.isEmpty((CharSequence) images.getUSER_PICTURE2())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE2());
            }
            if (!TextUtils.isEmpty((CharSequence) images.getUSER_PICTURE3())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE3());
            }
            if (!TextUtils.isEmpty(images.getUSER_PICTURE4())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE4());
            }
            if (!TextUtils.isEmpty(images.getUSER_PICTURE5())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE5());
            }
            if (!TextUtils.isEmpty(images.getUSER_PICTURE6())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE6());
            }
            if (!TextUtils.isEmpty(images.getUSER_PICTURE7())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE8());
            }
            if (!TextUtils.isEmpty(images.getUSER_PICTURE8())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE8());
            }
            if (!TextUtils.isEmpty(images.getUSER_PICTURE9())) {
                mUrls.add(images.getURL() + images.getUSER_PICTURE9());
            }
        }
        return mUrls;
    }
}
